﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.IO;
using System.Text;

public partial class design_page_student_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_show_post = new DATA_operate();   //创建数据库操作类对象
   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["stuid"];
            string str_sid = cookie.Value.ToString();
            string sql_show_all_post = @"select _post_id,_post_name,_addresser_id,_addresser_name,_send_time,_read_count from _tran_post where _receiver_id is null order by _send_time desc";
            string sql_show_my_posy = @"select _post_id,_addresser_id,_addresser_name,_send_time from _tran_post  where _receiver_id='" + str_sid + @"' order by _send_time desc";
            data_operate_in_show_post.Data_bind(sql_show_all_post, GridView_show_all_post, "_post_id");
            data_operate_in_show_post.Data_bind(sql_show_my_posy, GridView_show_my_post, "_send_time");

            for (int i = 0; i <= GridView_show_my_post.Rows.Count - 1; i++)
            {
                GridViewRow gvr = GridView_show_my_post.Rows[i];
                string str_id = ((Label)gvr.FindControl("dLabel_my_postid")).Text;
                string Is_read = data_operate_in_show_post.dataread("_tran_post", "_read_count", "_post_id", str_id);
                string Is_type = data_operate_in_show_post.dataread("_tran_post", "_post_name", "_post_id", str_id);
                Is_read = Is_read.Replace(" ", "");
                
             
                if (Is_read == "0")
                {
                    ((Label)gvr.FindControl("dLabel_is_read")).Text = "未读";
                    if(Is_type=="***未交作业提醒消息***")
                        ((Label)gvr.FindControl("dLabel_is_read")).Text = "未交作业提醒消息-未读";
                }
             
                else 
                {
                    ((Label)gvr.FindControl("dLabel_is_read")).Text = "已读";
                    if (Is_type == "***未交作业提醒消息***")
                        ((Label)gvr.FindControl("dLabel_is_read")).Text = "未交作业提醒消息-已读";
                }
            }
        }
       
    
    }
    protected void Gridview_show_all_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch(e.CommandName)
        {
            case "all_post_read":   //阅读详细


                Label1_student_answer.Visible = true;
                TextBox2.Visible = true;
                Button3.Visible = true;

                 int index = Convert.ToInt32(e.CommandArgument);
                 GridViewRow gvr = GridView_show_all_post.Rows[index];
                 string str_postid = ((Label)gvr.FindControl("dLabel_postid")).Text;    //作业ID
                 string sql_read_all_post = data_operate_in_show_post.dataread("_tran_post", "_post_content","_post_id", str_postid);//完成的内容
                 TextBox2.Text = sql_read_all_post;   //赋值到作业完成内容文本框            

                 if (Label2.Text == "隐藏的标志是否要添加查阅次数")
                {
                     Label2.Text = "已查阅一次";
                     string _read_count = data_operate_in_show_post.dataread("_tran_post", "_read_count", "_post_id", str_postid);//发布人姓名
                     int count=int.Parse(_read_count)+1;
                     _read_count =""+count;
                     if(data_operate_in_show_post.Data_update("_tran_post", "_read_count", _read_count, "_post_id", str_postid))
                     {
                         //Response.Redirect(Request.RawUrl);
                     }

                }


                 break;
        }
    }
    protected void Gridview_show_my_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "my_post_read":

                Label1_student_answer.Visible = true;
                TextBox2.Visible = true;
                Button3.Visible = true;

                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow gvr = GridView_show_my_post.Rows[index];
                string str_my_postid = ((Label)gvr.FindControl("dLabel_my_postid")).Text;    //作业ID


                string Is_read = data_operate_in_show_post.dataread("_tran_post", "_read_count", "_post_id", str_my_postid);
              
                int count = int.Parse(Is_read) + 1;
                Is_read = "" + count;
                if (data_operate_in_show_post.Data_update("_tran_post", "_read_count", Is_read, "_post_id", str_my_postid))
                {
                         //Response.Redirect(Request.RawUrl);
                }



                
               
                 string sql_read_my_post = data_operate_in_show_post.dataread("_tran_post", "_post_content","_post_id", str_my_postid);//完成的内容
                 TextBox2.Text = sql_read_my_post;   //赋值到作业完成内容文本框           
                break;
            case "my_post_return":

                Label_teacher_answer.Visible = true;
                TextBox4.Visible = true;
                Button2.Visible = true;
                Button3.Visible = true;

                int index1 = Convert.ToInt32(e.CommandArgument);
                GridViewRow gvr1 = GridView_show_my_post.Rows[index1];
                string _receiver_id = ((Label)gvr1.FindControl("dLabel_detail_stuid")).Text;    //接收者ID
                string _receiver_name = ((Label)gvr1.FindControl("dLabel_detail_stuid")).Text;    //接受者姓名
                extren_stuid.Text = _receiver_id;
                extren_homeid.Text = _receiver_name;

              
                break;
            case "my_post_detele":
                 int index2 = Convert.ToInt32(e.CommandArgument);
                 GridViewRow gvr2 = GridView_show_my_post.Rows[index2];
                 string str_postid = ((Label)gvr2.FindControl("dLabel_my_postid")).Text;    //作业ID
                 if(!data_operate_in_show_post.datadelete("_tran_post", "_post_id",str_postid))
                 {
                     Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('删除失败');", true);
                 }
                 else
                 {
                     Response.Redirect(Request.RawUrl);
                 }
                break;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)   //回复
    {
        if (TextBox4.Text != "")
        {
            string _post_id = Make_tran_post_id();
            HttpCookie cookie = Request.Cookies["stuid"];
            string _addresser_id = cookie.Value.ToString();   //发布人ID
            string _addresser_name = data_operate_in_show_post.dataread("_student", "_Sname", "_Sid", _addresser_id);//发布人姓名


            string _receiver_id = extren_stuid.Text;    //接收者ID
            string _receiver_name = extren_homeid.Text;    //接受者姓名
            string _post_name = TextBox5.Text;
            string _post_content = TextBox4.Text;
            string _send_time = DateTime.Now.ToLocalTime().ToString();

            string sql_send = _post_id + "," + _addresser_id + "," + _addresser_name + "," + _receiver_id + "," + _receiver_name + "," + _post_name + "," + _post_content + "," + _send_time;

            if (data_operate_in_show_post.dataadd("_tran_post", "_post_id,_addresser_id,_addresser_name,_receiver_id,_receiver_name,_post_name,_post_content,_send_time", sql_send))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送成功');", true);

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送失败');", true);

            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('不能发送空消息');", true);

        }
    }
    protected void Button3_Click(object sender, EventArgs e)  //返回
    {
        /*
        Label1_student_answer.Visible = false;
        TextBox2.Visible = false;
        Label_teacher_answer.Visible = false;
        TextBox4.Visible = false;
        Button2.Visible = false;
        Button3.Visible = false;
         * */
        Response.Redirect(Request.RawUrl);
    }



    private string Make_tran_post_id()   //生成作业ID
    {

       
        string numA = data_operate_in_show_post.dataread("MAX(_post_id)", "_tran_post", true);   //获得站内邮iD
        if (numA == null)
        {
            numA = "00000000000000";
        }
        string numB = "1";   //最大ID 号加1
        numA = numA.Replace(" ", "");

        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }


    protected void gridview_showall_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["stuid"];
        string str_sid = cookie.Value.ToString();
        string sql_show_all_post = @"select _post_id,_post_name,_addresser_id,_addresser_name,_send_time,_read_count from _tran_post where _receiver_id is null order by _send_time desc";
     
        data_operate_in_show_post.Data_bind(sql_show_all_post, GridView_show_all_post, "_post_id");

    }
    protected void gridvied_showmy_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["stuid"];
        string str_sid = cookie.Value.ToString();     
        string sql_show_my_posy = @"select _post_id,_addresser_id,_addresser_name,_send_time from _tran_post  where _receiver_id='" + str_sid + @"' order by _send_time desc";
        data_operate_in_show_post.Data_bind(sql_show_my_posy, GridView_show_my_post, "_send_time");
    }
}